<?php

//require_once("../globals.php");
require_once("../../globals.php");
require_once("$srcdir/patient.inc");

$request = $_REQUEST;



//echo "<pre>";
//	print_r($request);
//echo "</pre>";

//getpid
$pid = $_SESSION['pid'];
//check if user has data initialized
$query = "SELECT count(*) num FROM odontograma WHERE pid = $pid";
$res = sqlStatement($query);
$row = sqlFetchArray($res);
if($row['num']==0){
	$query = "INSERT INTO odontograma (pid) values ( $pid ) ";
	sqlStatement($query);
}else{
	//aready added
}
//get oid
$query = 'SELECT oid FROM odontograma WHERE pid = ' . $pid;
$res = sqlStatement($query);
$row = sqlFetchArray($res);
$oid = $row['oid'];

if( $request['action']=== 'get_history'){
	$query = 'SELECT oid FROM odontograma WHERE pid = ' . $pid;
	$res = sqlStatement($query);
	$row = sqlFetchArray($res);
	$oid = $row['oid'];

	echo "<table style='border: 1px solid black'>";
	echo "<tr>  <th> Date </th> <th> Th </th> <th> Surf </th> <th> Dx </th> <th> Description </th> <th> Stat </th>  </tr>";
//	$query = " SELECT * FROM odontograma_procedimientos odp inner join o_procedimientos op on (opd.proc_id = op.id)  WHERE oid = $oid ";

	$query = " SELECT * FROM odontograma_procedimientos odp WHERE oid = $oid ";
	//echo $query;
	$res = sqlStatement($query);
	while ($row = sqlFetchArray($res)){
		echo "<tr>";
			//echo "<td>".$row['id']."</td>";
			echo "<td>".$row['date']."</td>";
			echo "<td>".$row['tid']."</td>";
			echo "<td>".$row['superficie']."</td>";
			echo "<td>".$row['diagnostico']."</td>";
			echo "<td>".$row['descripcion']."</td>";
			echo "<td>".$row['estado']."</td>";
		echo "</tr>";
	}
	
	echo "</table>";
}
if( $request['action'] === 'piezas_dentales'){
	//guardar o eliminar piezas dentale
	
	$query = 'SELECT oid FROM odontograma WHERE pid = ' . $pid;
	$res = sqlStatement($query);
	$row = sqlFetchArray($res);
	$oid = $row['oid'];
	$tid = $request['tid'];
	if( $request['add']==='true' ){
		//agregar el diente al registtro
		$query = "insert into odontograma_piezas_dentales (oid, tid) values ( $oid, $tid )";
		//echo $query;
		sqlStatement($query);
		
	}
	if( $request['remove']==='true' ){
		//buscar el diente y sacarlo del registro
		$query =  "DELETE FROM odontograma_piezas_dentales WHERE tid = $tid AND oid = $oid";
		//echo $query;
		sqlStatement($query);
	}
}

if( $request['action']=== 'movimiento_piezas'){
//para los shift y rotaciones

	$tid = $request['tid'];
	
	//$query = "SELECT * FROM odontograma_piezas_dentales WHERE tid = $tid AND oid = $oid";
	//$res = sqlStatement($query);
	//$row = sqlFetchArray($res);
	
	check_tid($tid);
	$val = $request['number'];
	$val2 = $request['number2'];
	if($request['movimiento']=='shift'){
		switch($request['tipo']){
			case 1:
				$query = "UPDATE odontograma_piezas_dentales SET shift_mesial = $val, shift_mesial2 = $val2 WHERE tid = $tid AND oid = $oid";
			break;
			
			case 2:
				$query = "UPDATE odontograma_piezas_dentales SET shift_occlusal = $val, shift_occlusal2 = $val2 WHERE tid = $tid AND oid = $oid";
			break;
			
			case 3:
				$query = "UPDATE odontograma_piezas_dentales SET shift_labial = $val, shift_labial2 = $val2 WHERE tid = $tid AND oid = $oid";
			break;
		}
		echo $query;
		sqlStatement($query);
	}
	if($request['movimiento']=='rotate'){
		switch($request['tipo']){
			case 1:	
				$query = "UPDATE odontograma_piezas_dentales SET rotate = $val, rotate2 = $val2 WHERE tid = $tid AND oid = $oid";
			break;
			
			case 2:
				$query = "UPDATE odontograma_piezas_dentales SET mesial_tip = $val, mesial_tip2 = $val2 WHERE tid = $tid AND oid = $oid";
			break;
			
			case 3:
				$query = "UPDATE odontograma_piezas_dentales SET labial_tip = $val, labial_tip2 = $val2 WHERE tid = $tid AND oid = $oid";
			break;
		}
		echo $query;
		sqlStatement($query);
	}
	//revisar si ya tiene movimientos

//sino solo agregarle los valores

}

if( $request['action']=== 'add_del_procedimiento'){
//para agregar los nuevos procedimient
	if( $request['tipo'] === 'add' ){
		//get surface
		$surface = $request['secciones'];
		$surface_p = explode(";", $surface);
		$surface_total = '';	
		foreach ( $surface_p as $section ){
			switch ( $section ){
				case 1:
					$surface_total .= 'B';
				break;
				case 2:
					$surface_total .= 'W';
				break;
				case 3:
					$surface_total .= 'M';
				break;
				case 4:
					$surface_total .= 'O';
				break;
				case 5:
					$surface_total .= 'D';
				break;
				case 6:
					$surface_total .= 'L';
				break;
			}
		}		
		//get tids
		if( $request['tid']==''){
			$teeth = '-1';
			$procedimiento = $request['proc'];
			$date = ' NOW() ';
			$descripcion = $request['descripcion'];
			$query = "INSERT INTO odontograma_procedimientos ( oid, tid, date, procedimiento, descripcion) values ( $oid, $teeth, $date, $procedimiento,\"$descripcion\" ) ";
			echo $query;
			sqlStatement($query);
		}else{			
			$tid = $request['tid'];
			$tids = explode(";" , $tid);
			foreach ( $tids as $teeth ){
				$date = ' NOW() ';
				$status = $request['status'];
				$diagnostico = $request['diagnos'];
				$prognosis = $request['prognosis'];
				$procedimiento = $request['proc'];
				$descripcion = $request['descripcion'];
				//$priority = $request['priority'];
				$query = "INSERT INTO odontograma_procedimientos ( oid, tid, date, procedimiento, superficie, estado, diagnostico, descripcion) values ( $oid, $teeth, $date, $procedimiento, \"$surface_total\", \"$status\", \"$diagnostico\", \"$descripcion\" ) ";
				echo $query;
				sqlStatement($query);
			}	
		}
	}
	
	if( $request['tipo'] == 'delete'){
		$query = "DELETE FROM odontograma_procedimientos WHERE pid = ";
		sqlStatement($query);
	}
}

if( $request['action']== ''){

}

function check_tid($tiid){
	global $pid, $oid;
	$query = 'SELECT oid FROM odontograma WHERE pid = ' . $pid;
	$res = sqlStatement($query);
	$row = sqlFetchArray($res);
	$oid = $row['oid'];

	$query = "SELECT count(*) num FROM odontograma_piezas_dentales WHERE oid = $oid AND tid = ".$tiid;
	$res = sqlStatement($query);
	$row = sqlFetchArray($res);
	
	if( $row['num'] == 0){
		echo $query;
		$query = "INSERT INTO odontograma_piezas_dentales (oid, tid) VALUES ($oid, $tiid)";
		sqlStatement($query);
	}
}

?>